package com.bruan.threadpool;

import java.util.concurrent.*;

public class ThreadPoolTest2 {
    public static void main(String[] args) {
        //创建线程池对象
//        ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler)
//        ThreadPoolExecutor pool = new ThreadPoolExecutor(3,5,8, TimeUnit.SECONDS,
//                new LinkedBlockingDeque<>(4), Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());

        //使用Executors创建一个线程池对象
        ExecutorService pool = Executors.newFixedThreadPool(3);

        //核心线程对象应该配置多少
        //计算密集型 ：cup核数+1
        //io密集型：cpu核数*2

        try {
            Future<String> f1 = pool.submit(new MyCallable(100));
            Future<String> f2 = pool.submit(new MyCallable(200));
            Future<String> f3 = pool.submit(new MyCallable(300));
            Future<String> f4 = pool.submit(new MyCallable(400));
            Future<String> f5 = pool.submit(new MyCallable(500));
            System.out.println(f1.get());
            System.out.println(f2.get());
            System.out.println(f3.get());
            System.out.println(f4.get());
            System.out.println(f5.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

}
